﻿using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using NiuX.Csv.Utils;
using NiuX.Utils.Csv.Quantum;
using TinyCsvParser.Mapping;

namespace NiuX.Utils.Csv.Utils;

/// <summary>
/// Csv 工具类
/// </summary>
public static class CsvUtils
{
    #region Read

    #region QuantumRead

    /// <summary>
    /// 量子阅读
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path"></param>
    /// <param name="hasheader"></param>
    /// <param name="delimiter"></param>
    /// <param name="mapper"></param>
    /// <returns></returns>
    public static List<T> QuantumRead<T>(string path, bool hasheader, char delimiter, QuantumCsv.ToObject<T> mapper) => QuantumCsv.Read(path, hasheader, delimiter, mapper);

    /// <summary>
    /// 量子阅读
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path"></param>
    /// <param name="mapper"></param>
    /// <returns></returns>
    public static List<T> QuantumRead<T>(string path, QuantumCsv.ToObject<T> mapper) => QuantumRead(path, true, ',', mapper);

    #endregion


    public static List<T> ReadAsList<T>(string filePath) where T : new() => SimpleCsvUtility.Read<T>(filePath);

    public static List<T> ReadAsListWithCsvHelper<T>(string filePath) => CsvHelperUtility.ReadAsList<T>(filePath);

    public static Task<List<T>> ReadAsListWithCsvHelperAsync<T>(string filePath) => CsvHelperUtility.ReadAsListAsync<T>(filePath);

    public static List<T> ReadAsListWithCsvTfp<T>(string filePath) => CsvTfpUtility.ReadAsList<T>(filePath);

    public static Task<List<T>> ReadAsListWithCsvTfpAsync<T>(string filePath) => Task.Run(() => CsvTfpUtility.ReadAsList<T>(filePath));

    public static List<T> ReadAsListWithTinyCsv<T>(string filePath, CsvMapping<T> mapping) where T : class, new() => TinyCsvUtility.ReadAsList(filePath, mapping);

    public static Task<List<T>> ReadAsListWithTinyCsvAsync<T>(string filePath, CsvMapping<T> mapping) where T : class, new() => Task.Run(() => TinyCsvUtility.ReadAsList(filePath, mapping));

    #endregion

    #region Write

    public static void Write<T>(string filePath, IEnumerable<T> items) where T : new() => SimpleCsvUtility.Write(filePath, items);
    public static void Write(string filePath, DataTable table) => SimpleCsvUtility.Write(filePath, table);

#if NETSTANDARD2_1_OR_GREATER


    public static Task WriteAsync<T>(string filePath, IEnumerable<T> items) where T : new() => SimpleCsvUtility.WriteAsync(filePath, items);

    public static Task WriteAsync(string filePath, DataTable table) => SimpleCsvUtility.WriteAsync(filePath, table);

#endif


    public static void WriteWithCsvHelper<T>(string filePath, IEnumerable<T> items) => CsvHelperUtility.Write(filePath, items);

    public static Task WriteWithCsvHelperAsync<T>(string filePath, IEnumerable<T> items) => CsvHelperUtility.WriteAsync(filePath, items);

    #endregion

}